package com.itheima.day5;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;

//注解（value)
@WebServlet("/Demo01")
public class PersonServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //根据一个相对WEB文件夹路径寻找磁盘此文件真实路径
        String realPath = servletContext.getRealPath("person.txt");
        //读取
        BufferedReader br = new BufferedReader(new FileReader(realPath, Charset.forName("utf-8")));
//        int len;
        while (true) {
            String len = br.readLine();
            if (len == null) {
                break;
            }
            System.out.println(len);
        }
        br.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
